﻿using LcfContols.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LcfContols
{
    public class Loading:Label
    {
        #region 变量
        private Size TextSize;
        #endregion

        [Browsable(false)]
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }

            set
            {
                base.AutoSize = value;
            }
        }

        public Loading(){
            SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
            Text = "数据加载中....";
            Image = Resources.loading;
            AutoSize = false;
            CheckSize();
        }

        public void CheckSize()
        {
            TextSize = TextRenderer.MeasureText(Text, Font);
            Size = new Size(TextSize.Width + Image.Width + 30, Math.Max(Image.Height, TextSize.Height)+10);
            MinimumSize = Size;
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            CheckSize();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            CheckSize();
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int height = ClientRectangle.Height;
            ImageAnimator.UpdateFrames(Image);
            e.Graphics.DrawImageUnscaled(Image,10, (height-Image.Height)/2);
            using(Brush b=new SolidBrush(ForeColor)){
                e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                e.Graphics.DrawString(Text, Font, b, 10 + Image.Width + 10,(height- TextSize.Height)/2);
            }
        }

    }
}
